package lxt.dao.impl;

import java.util.ArrayList;
import java.util.List;

import lxt.dao.DriverDao;
import lxt.entity.DriverInfo;
/**
 * 驾驶员管理dao
 * @author Lit
 *
 */
@SuppressWarnings("unchecked")
public class DriverDaoImpl extends ABseDaoImpl implements DriverDao {

	
	public List<Object> loadAllDriver(String name, String driverLicense,int offset, int length) {
		try{
			//查询的hql
			StringBuffer hql = new StringBuffer("select d,p from DriverInfo d,Parameter p where d.licenseType = p.id.paramKey And p.id.paramType = ? ");
			//存入字段
			ArrayList<Object> param = new ArrayList<Object>();
			param.add("licenseType");
			if(name!=null && !name.equals("")){
				hql.append(" And d.name like ?");
				param.add("%"+name+"%");
			}
			if(driverLicense!=null && !driverLicense.equals("")){
				hql.append(" And d.driverLicense like ?");
				param.add("%"+driverLicense + "%");
			}
			
			List<Object> drivers = (List<Object>) this.queryForPage(hql.toString(),param, offset, length);
			
			return drivers;
		}catch(Exception ex ){
			ex.printStackTrace();
			return null;
		}
		
		
	}

	public int loadAllDriverCount(String name, String driverLicense) {
		//查询的hql
		StringBuffer hql = new StringBuffer("select d,p from DriverInfo d,Parameter p where d.licenseType = p.id.paramKey And p.id.paramType = ? ");
		//存入字段
		ArrayList<Object> param = new ArrayList<Object>();
		param.add("licenseType");
		
		if(name!=null && !name.equals("")){
			hql.append(" And d.name like ?");
			param.add("%"+name+"%");
		}
		if(driverLicense!=null && !driverLicense.equals("")){
			hql.append(" And d.driverLicense like ?");
			param.add("%"+driverLicense + "%");
		}
		
		return this.getAllRowCount(hql.toString(), param);
	}

	/**
	 * 根据name查找驾驶员
	 */
	public List<DriverInfo> loadDriverByName(String name){
		StringBuffer hql = new StringBuffer("Select d from DriverInfo d where 1=1 ");
		List<DriverInfo> drivers ;
		if(name!=null&&!name.equals("")){
			hql.append(" And d.name = ? ");
			drivers = (List<DriverInfo>) this.getHibernateTemplate().find(hql.toString(),name);
		}
		else{			drivers = (List<DriverInfo>) this.getHibernateTemplate().find(hql.toString());
		}
		return drivers;
	}
}
